
#include "CAnimation.h"

CAnimation::CAnimation(){
	this->currentFrame = 0;
	this->maxFrames = 0;
	this->frameInc = 1;
	this->frameRate = 1000 / ANIM_FRAME_RATE;
	this->oldTime = 0;
	this->oscillate = false;
}

void CAnimation::OnAnimate(){
	
	if( (unsigned)(oldTime + frameRate) > SDL_GetTicks() )
		return;

	oldTime = SDL_GetTicks();

	currentFrame += frameInc;

	if(oscillate)
	{
		if(frameInc > 0)
		{
			if(currentFrame >= maxFrames-1)
				frameInc = -frameInc;
		}
		else
		{
			if(currentFrame <= 0)
				frameInc = -frameInc;
		}
	}
	else
	{
		if(currentFrame >= maxFrames-1)
			currentFrame = 0;
	}
}

void CAnimation::setFrameRate(int rate){
	frameRate = rate;
}

void CAnimation::setCurrentFrame(int frame){
	
	if(frame < 0 || frame >= maxFrames)
		return;

	currentFrame = frame;
}

int CAnimation::getCurrentFrame(){
	return currentFrame;
}